package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.Collections;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 论坛内容 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-10
 */
@Controller
@ResponseBody
@CrossOrigin
@Slf4j
@Api(tags = {"职业圈子论坛"})
@RequestMapping("/web/Bbs")
public class FullBbsController {

    @Autowired
    private IBbsService bbsService;

    @Autowired
    private ICatalogBbsService catalogBbsService;

    @Autowired
    private IBbsFollowService bbsFollowService;

    @Autowired
    private ISysAppLoginService loginService;

    @Autowired
    private IBbsFabulousService fabulousService;

    @GetMapping("/directoryList")
    public void directoryList(HttpServletResponse response) throws IOException {
        log.info(LogUtil.LogInfo("查询职业圈子论坛目录", new Date()));
        List<CatalogBbs> list = catalogBbsService.selectAll();
        writeJSON(list, response, "200", "成功");
    }

    @PostMapping("/postTopic")
    public void postTopic(HttpServletResponse response, Bbs bbs) throws IOException {
        log.info(LogUtil.LogInfo("发布论坛", new Date()));
        if (bbs.getLtText() != null && bbs.getLtTitle() != null) {
            bbs.setLtCreatetime(new Date());
            bbs.setParentId(0);
            bbs.setViewsCount(0);
            bbs.setLtId(UUIDUtils.getByUUId());
            bbsService.insertBbs(bbs);
            writeJSON("发布成功", response, "200", "成功");
        }
    }

    @GetMapping("/concerned")
    public void concerned(HttpServletResponse response, String userId, Integer pageNum, Integer pageSize) throws IOException {
        log.info(LogUtil.LogInfo("查询我关注的话题", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Bbs> list = bbsFollowService.selectMyDeTopic(userId);
        for (Bbs bbs : list) {
            SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
            bbs.setUserName(appLogin.getUserAccount());
            bbs.setUserIcoUrl(appLogin.getUserIco());

            List<Bbs> list1 = bbsService.selectByParentId(bbs.getId());
            bbs.setPinlunCount(list1.size());

            BbsFabulous bbsFabulous = new BbsFabulous();
            bbsFabulous.setBbsId(bbs.getId());
            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous);
            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
                bbs.setDianzanCount(bbsFabulous1.size());
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @PostMapping("/addAttention")
    public void addAttention(HttpServletResponse response, String userId, Integer bbsId) throws IOException {
        log.info(LogUtil.LogInfo("按钮添加关注", new Date()));
        if (userId != null && bbsId != null) {
            BbsFollow bbsFollow = new BbsFollow();
            bbsFollow.setUserId(userId);
            bbsFollow.setBbsId(bbsId);
            List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
            Map<String, String> map = new HashMap<>();
            if (bbsFollows != null && bbsFollows.size() > 0) {
                bbsFollowService.deleteBbsFollowByIds(bbsFollows.get(0).getId().toString());
                map.put("status", "0");
                List<BbsFollow> bbsFollows1 = bbsFollowService.selectByBbsId(bbsId);
                map.put("count", bbsFollows1.size() + "");
            } else {
                bbsFollow.setCreateTime(new Date());
                bbsFollowService.insertBbsFollow(bbsFollow);
                map.put("status", "1");
                List<BbsFollow> bbsFollows1 = bbsFollowService.selectByBbsId(bbsId);
                map.put("count", bbsFollows1.size() + "");
            }
            writeJSON(map, response, "200", "成功");
        }
        writeJSON("未登录，请登录", response, "201", "成功");
    }

    @GetMapping("/initiated")
    public void initiated(HttpServletResponse response, String userId, Integer pageNum, Integer pageSize) throws
            IOException {
        log.info(LogUtil.LogInfo("查询我发起的话题", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Bbs> list = bbsService.selectByUserId(userId);
        for (Bbs bbs : list) {
            CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bbs.getCatalogId());
            bbs.setCatalogName(catalogBbs.getName());
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/topicDetails")
    public void topicDetails(HttpServletResponse response, Integer id, String userId) throws IOException {
        log.info(LogUtil.LogInfo("查询话题详情", new Date()));
        Bbs bbs = bbsService.selectBbsById(id);
        CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bbs.getCatalogId());
        bbs.setCatalogName(catalogBbs.getName());
        if (bbs.getViewsCount() == null) {
            bbs.setViewsCount(1);
        } else {
            bbs.setViewsCount(bbs.getViewsCount() + 1);
        }

        bbsService.updateBbs(bbs);
        SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
        if (appLogin != null) {
            bbs.setUserName(appLogin.getUserAccount());
            bbs.setUserIcoUrl(appLogin.getUserIco());

            if (userId != null && userId.length() > 0) {
                BbsFabulous fabulous = new BbsFabulous();
                fabulous.setUserId(userId);
                fabulous.setBbsId(bbs.getId());

                List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
                if (bbsFabulous != null && bbsFabulous.size() > 0) {
                    bbs.setDoYouLikeIt("1");
                } else {
                    bbs.setDoYouLikeIt("0");
                }

                BbsFollow bbsFollow = new BbsFollow();
                bbsFollow.setBbsId(bbs.getId());
                bbsFollow.setUserId(userId);

                List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
                if (bbsFollows != null && bbsFollows.size() > 0) {
                    bbs.setShifouguanzhu("1");
                } else {
                    bbs.setShifouguanzhu("0");
                }
            }

            List<Bbs> list1 = bbsService.selectByParentId(bbs.getId());
            bbs.setPinlunCount(list1.size());

            BbsFabulous bbsFabulous2 = new BbsFabulous();
            bbsFabulous2.setBbsId(bbs.getId());
            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous2);
            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
                bbs.setDianzanCount(bbsFabulous1.size());
            }
        }
        writeJSON(bbs, response, "200", "成功");
    }

    @GetMapping("/detailsList")
    public void detailsList(HttpServletResponse response, Integer BbsId, Integer pageNum, Integer pageSize) throws
            IOException {
        log.info(LogUtil.LogInfo("查询底下详情列表", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Bbs> list = bbsService.selectByParentId(BbsId);
        for (Bbs bbs : list) {
            SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
            if (appLogin != null) {
                bbs.setUserName(appLogin.getUserAccount());
                bbs.setUserIcoUrl(appLogin.getUserIco());
                BbsFabulous fabulous = new BbsFabulous();
                fabulous.setUserId(bbs.getLtUserid());
                fabulous.setBbsId(bbs.getId());

                List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
                if (bbsFabulous != null && bbsFabulous.size() > 0) {
                    bbs.setDoYouLikeIt("1");
                } else {
                    bbs.setDoYouLikeIt("0");
                }
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @PostMapping("/topicReviewAdded")
    public void topicReviewAdded(HttpServletResponse response, Bbs bbs) throws IOException {
        log.info(LogUtil.LogInfo("添加话题详情", new Date()));
        if (bbs.getLtText() != null) {
            bbs.setLtCreatetime(new Date());
            String byUUId = UUIDUtils.getByUUId();
            bbs.setLtId(byUUId);
            bbsService.insertBbs(bbs);
            writeJSON(bbsService.selectByUuId(byUUId), response, "200", "成功");
        }
    }

    @GetMapping("/list")
    public void list(HttpServletResponse response, Integer pageNum, Integer pageSize, Integer catalogId, String
            userId) throws IOException {
        log.info(LogUtil.LogInfo("查询论坛列表", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Bbs> list = bbsService.selectByCatalogIdAndParentId(catalogId);
        for (int i = 0; i < list.size(); i++) {
            List<Bbs> list1 = bbsService.selectByParentId(list.get(i).getId());
            list.get(i).setPinlunCount(list1.size());
            if (userId != null && userId.length() > 0) {
                BbsFollow bbsFollow = new BbsFollow();
                bbsFollow.setBbsId(list.get(i).getId());
                bbsFollow.setUserId(userId);

                List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
                if (bbsFollows != null && bbsFollows.size() > 0) {
                    list.get(i).setShifouguanzhu("1");
                } else {
                    list.get(i).setShifouguanzhu("0");
                }
            }

            BbsFabulous bbsFabulous = new BbsFabulous();
            bbsFabulous.setBbsId(list.get(i).getId());
            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous);
            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
                list.get(i).setDianzanCount(bbsFabulous1.size());
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/featuredTopics")
    public void featuredTopics(HttpServletResponse response, Integer pageNum, Integer pageSize, String userId) throws
            IOException {
        log.info(LogUtil.LogInfo("社区精选话题，我关注的话题", new Date()));
        if (userId != null && userId.length() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<Bbs> bbs = bbsFollowService.selectMyDeTopic(userId);
            for (Bbs bb : bbs) {
                CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bb.getCatalogId());
                bb.setCatalogName(catalogBbs.getName());
                bb.setShifouguanzhu("1");
                List<Bbs> list = bbsService.selectByParentId(bb.getId());
                bb.setPinlunCount(list.size());
            }
            writeJSON(new PageInfo<>(bbs), response, "200", "成功");
        } else {
            PageHelper.startPage(pageNum, pageSize);
            List<Bbs> list = bbsService.selectByParentId(0);
            for (Bbs bbs : list) {
                CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bbs.getCatalogId());
                bbs.setCatalogName(catalogBbs.getName());
            }
            writeJSON(new PageInfo<>(list), response, "200", "成功");
        }
    }

    @GetMapping("/randomTopic")
    public void randomTopic(HttpServletResponse response, Integer count) throws IOException {
        log.info(LogUtil.LogInfo("推荐话题，一周话题榜", new Date()));
        List<Bbs> list = bbsService.selectByParentId(0);
        Collections.shuffle(list);
        if (count > list.size()) {
            writeJSON(list, response, "200", "成功");
        } else {
            list = list.subList(0, count);
            writeJSON(list, response, "200", "成功");
        }
    }

    @PostMapping("/addLike")
    public void addLike(HttpServletResponse response, String userId, Integer bbsId) throws IOException {
        log.info(LogUtil.LogInfo("点赞", new Date()));
        BbsFabulous fabulous = new BbsFabulous();
        fabulous.setUserId(userId);
        fabulous.setBbsId(bbsId);
        if (fabulous.getUserId() != null) {
            List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
            Map<String, String> map = new HashMap<>();
            if (bbsFabulous != null && bbsFabulous.size() > 0) {
                fabulousService.deleteBbsFabulousByIds(bbsFabulous.get(0).getId().toString());
                map.put("status", "0");
                List<BbsFabulous> list = fabulousService.selectByBbsId(bbsId);
                map.put("count", list.size() + "");
            } else {
                fabulous.setCreateTime(new Date());
                fabulousService.insertBbsFabulous(fabulous);
                map.put("status", "1");
                List<BbsFabulous> list = fabulousService.selectByBbsId(bbsId);
                map.put("count", list.size() + "");
            }
            writeJSON(map, response, "200", "成功");
        } else {
            writeJSON("未登录，请登录", response, "201", "成功");
        }
    }

    @GetMapping("/search")
    public void search(HttpServletResponse response, String name, Integer pageSize, Integer pageNum) throws
            IOException {
        log.info(LogUtil.LogInfo("搜索", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Bbs> list = bbsService.selectByLike(name);
        for (Bbs bbs : list) {
            CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bbs.getCatalogId());
            bbs.setCatalogName(catalogBbs.getName());
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }
}
